package com.example.demo.controller;

import com.example.demo.pojo.Invitation;
import com.example.demo.pojo.ReplyDetail;
import com.example.demo.service.InvitationService;
import com.example.demo.service.ReplyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class TestController {
    @Autowired
    InvitationService invitationService;
    @Autowired
    ReplyDetailService replyDetailService;
    @RequestMapping("toshow")
    public String toshow(){
        return "index";
    }
    @RequestMapping("toreply")
    public String toreply(){
        return "reply";
    }
    @RequestMapping("getinvitations")
    @ResponseBody
    public Object getinvitations(Invitation invitation,@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "5") int pageSize){
        PageHelper.offsetPage((pageNo-1)* pageSize, pageSize);
        PageHelper.startPage(pageNo,pageSize,"createdate desc");
        if (invitation==null){
            invitation=new Invitation();
        }
        List<Invitation> invitations = invitationService.getByContext(invitation);
        PageInfo<Invitation> pageInfo= new PageInfo<>(invitations);
        return pageInfo;
    }
    @RequestMapping("getreplysbyinvid")
    @ResponseBody
    public Object getreplysbyinvid(Integer invid){
       return replyDetailService.getAllByInvid(invid);
    }
    @RequestMapping("addreplysbyinvid")
    @ResponseBody
    public Object addreplysbyinvid(ReplyDetail replyDetail){
        Date date=new Date();
        replyDetail.setCreatedate(date);
        if("".equals(replyDetail.getAuthor())){
            replyDetail.setAuthor("匿名用户");
        }
        return replyDetailService.addReplyDetail(replyDetail)>0?"ok":"no";
    }
}
